package com.carrot.uaa.infrastructure.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.carrot.uaa.constant.MathConstants;
import com.carrot.uaa.constant.SystemTenantIdConstant;
import com.carrot.uaa.entity.OAuth2User;
import com.carrot.uaa.entity.SysUser;
import com.carrot.uaa.entity.TenantUser;
import com.carrot.uaa.enums.UserTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 用户数据
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final SysUserService sysUserService;

    private final TenantUserService iTenantUserService;

    @Override
    public OAuth2User getUserByAccount(String account) {
        OAuth2User OAuth2User;
        //查运营用户数据
        LambdaQueryWrapper<SysUser> sysWrapper = Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getDeleted, MathConstants.ZERO)
                .and(w-> w.eq(SysUser::getEmail, account)
                .or()
                .eq(SysUser::getAccount, account));

        SysUser sysUser = sysUserService.getOne(sysWrapper);
        if(Objects.nonNull(sysUser)){
             OAuth2User = BeanUtil.copyProperties(sysUser, OAuth2User.class);
             OAuth2User.setTenantId(SystemTenantIdConstant.TENANT_ID.toString());
             OAuth2User.setUserType(UserTypeEnum.OPERATOR.toString());
             OAuth2User.setUid(sysUser.getId().toString());
             return OAuth2User;
        }

        //查租户用户数据
        LambdaQueryWrapper<TenantUser> TenantWrapper = Wrappers.lambdaQuery(TenantUser.class)
                .eq(TenantUser::getDeleted, MathConstants.ZERO).and(w->w.eq(TenantUser::getEmail, account)
                        .or().eq(TenantUser::getAccount, account));
        TenantUser tenantUser = iTenantUserService.getOne(TenantWrapper);

        if(Objects.nonNull(tenantUser)){
            OAuth2User = BeanUtil.copyProperties(tenantUser, OAuth2User.class);
            OAuth2User.setUserType(UserTypeEnum.TENANT.toString());
            OAuth2User.setTenantId(String.valueOf(tenantUser.getTenantId()));
            OAuth2User.setUid(tenantUser.getId().toString());
           return OAuth2User;
        }

        return null;
    }

    @Override
    public void updateUserLoginTimeAndIp(String uid, String ip, String userType) {
        LocalDateTime now=LocalDateTime.now();
        if(UserTypeEnum.TENANT.toString().equals(userType)){
            iTenantUserService.update(Wrappers.lambdaUpdate(TenantUser.class)
                    .set(TenantUser::getLoginIp,ip).set(TenantUser::getLastLoginTime,now)
                    .eq(TenantUser::getId,uid));
        }else if(UserTypeEnum.OPERATOR.toString().equals(userType)){
            sysUserService.update(Wrappers.lambdaUpdate(SysUser.class)
                    .set(SysUser::getLoginIp,ip).set(SysUser::getLastLoginTime,now)
                    .eq(SysUser::getId,uid));
        }
    }


}
